package cn.edu.ecnu.gis.client.gadgets;

import cn.edu.ecnu.gis.client.ltapi.localsearch.LTLocalSearchRequestHandle;
import cn.edu.ecnu.gis.client.model.Gadget;
import cn.edu.ecnu.gis.client.model.GadgetClass;
import cn.edu.ecnu.gis.client.view.GoogleMap;
import com.google.gwt.core.client.GWT;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ImageBundle;
import com.mapitz.gwt.googleMaps.client.GControl;
import com.mapitz.gwt.googleMaps.client.GLatLng;
import com.mapitz.gwt.googleMaps.client.GMap2;
import com.mapitz.gwt.googleMaps.client.GMap2Widget;

public class GoogleMapsGadget2 extends Gadget implements LTLocalSearchRequestHandle {

    HTML controlHtml = new HTML();

    public static class Class extends GadgetClass {

        public Class() {
            super("地图googlemap2", false);
            this.setGadgetType(1);
        }

        public Gadget newGadget() {
            return new GoogleMapsGadget2(this);
        }
    }
    GMap2Widget mapWidget = new GMap2Widget("300", "300", new GLatLng(37.4419, -122.1419), 11, null);
    private GoogleMap map = new GoogleMap(new FlowPanel());
 //   private GMap2 gmap2 = new GMap2(map.getElement());

    public GMap2 getGMap() {
        return mapWidget.getGmap();
    }

    protected GoogleMapsGadget2(Class c) {
        super(c);
        controlHtml.setHTML("<div id='CONTROL' style='height:100px'></div>");
        
        initWidget(mapWidget);
        GMap2 gmaps = mapWidget.getGmap();        
        gmaps.setCenter(new GLatLng(37.4419, -122.1419));
       // GControlPosition gp = new GControlPosition(GControlAnchor.G_ANCHOR_BOTTOM_LEFT(), new GSize(100, 100));
        //GControl.GSmallMapControl();
        //GControl gcontrol = new GControl((Widget) controlHtml, gp);
        gmaps.addControl(GControl.GSmallMapControl());
        //gmap2.setCenter(new GLatLng(37.4419, -122.1419), 3);
        //map.setCenter( 37.4419, -122.1419 );

    }

    public interface BoundImage extends ImageBundle {

        AbstractImagePrototype inbox();
    }

    public AbstractImagePrototype getBoundImage() {
        BoundImage images = (BoundImage) GWT.create(BoundImage.class);
        return images.inbox();
    }

    public void onRequestComplete(String result) {
        JSONObject searchResult = (JSONObject) JSONParser.parse(result);
        //JSONArray entries = (JSONArray) searchResult.get("entries");
        JSONArray searchPoints = (JSONArray) searchResult.get("searchPoints");

        for (int i = 0; i < searchPoints.size(); i++) {
            JSONObject item = (JSONObject) searchPoints.get(i);
            JSONString id = (JSONString) item.get("id");
            JSONString url = (JSONString) item.get("url");
            JSONString name = (JSONString) item.get("name");
            JSONArray points = (JSONArray) item.get("point");
            //map.addMarker(points);
            HTML html = new HTML("<a href='" + url.stringValue() + "' target='_blank'>" + name.stringValue() + "</a>");
            html.setStyleName("News-Item");
            //list.add( html );
        }
    }

    public void onRequestFailed(String error) {
        HTML html = new HTML(" error ");
        html.setStyleName("News-Item");
    }
}
